package lk.icta.core.service.impl;

import lk.icta.core.dao.TaxationDAO;
import lk.icta.core.entity.Taxation;
import lk.icta.core.service.TaxationManagementService;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * @author Supun Viraj Rathnayaka
 */

public class TaxationManagementServiceImpl implements TaxationManagementService {

    private TaxationDAO taxationDAO;

    public TaxationManagementServiceImpl(TaxationDAO taxationDAO){
        this.taxationDAO = taxationDAO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Taxation> getAllTaxationData() {
        return taxationDAO.getAllTaxationData();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addTaxation(Taxation taxation, User user, String language) {
        Taxation t = taxationDAO.getPreviousTaxation();
        if(t != null){
            Calendar cal = Calendar.getInstance();
            cal.setTime(taxation.getStartDate());
            cal.add(Calendar.DATE, -1);
            t.setEndDate(cal.getTime());
            taxationDAO.updateTaxation(t, user);
        }    
        taxationDAO.addTaxation(taxation, user);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public Taxation getRecentTaxation() {
        return taxationDAO.getPreviousTaxation();
    }
}
